//
//  AddFriendViewController.m
//  BaseAPP
//
//  Created by Evisu on 16/12/20.
//  Copyright © 2016年 evisum. All rights reserved.
//

#import "AddFriendViewController.h"
#import "AddFriendView.h"
#import "AddFriendSearchView.h"

@interface AddFriendViewController ()<UISearchBarDelegate, UIGestureRecognizerDelegate>{

    //右滑返回开关
    BOOL _allowSideBack;
}

//添加朋友View
@property (nonatomic, strong)AddFriendView *addFriendView;

//点击添加朋友的搜索框，切换的详情View
@property (nonatomic, strong)AddFriendSearchView *addFriendSearchView;

@end

@implementation AddFriendViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    
    [self initViews];
    [self layoutViews];
}

- (void)initViews {

    self.addFriendView = [[AddFriendView alloc] init];
    [self.view addSubview:self.addFriendView];
    
    __block typeof(self) weakSelf = self;
    //点击搜索框回调
    //点击搜索框隐藏当前页，显示搜索详情页
    self.addFriendView.searchBlock = ^(){
        
        weakSelf.addFriendSearchView = [[AddFriendSearchView alloc] init];
        UIColor *bgColor = KBACKGROUND_COLOR;
        weakSelf.addFriendSearchView.backgroundColor = [bgColor colorWithAlphaComponent:0.985];
                weakSelf.addFriendSearchView.searchBar.delegate = weakSelf;
        [weakSelf.view addSubview:weakSelf.addFriendSearchView];
        
        weakSelf.addFriendSearchView.frame = CGRectMake(0, 20, weakSelf.view.width_sd, weakSelf.view.height_sd-KSTATUSBAR_HEIGHT);
    };
}


- (void)layoutViews {

    self.addFriendView.sd_layout
    .topSpaceToView(self.view, KSTATUS_AND_NAVIGATION_HEIGHT)
    .leftEqualToView(self.view)
    .rightEqualToView(self.view)
    .bottomEqualToView(self.view);
    
    self.addFriendView.contentSize = CGSizeMake(self.view.width_sd, self.view.height_sd);
}

#pragma mark - 搜索详情页UISearchBar  Delegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    
    //显示取消按钮
    self.addFriendSearchView.searchBar.showsCancelButton = YES;
    
    UIButton *cancleButton = [searchBar valueForKey:@"cancelButton"];
    [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancleButton setTitleColor:HEXColor(@"65C96B") forState:UIControlStateNormal];
    cancleButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
    
    [self hideNavigationBar];
    
    return YES;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    
    searchBar.showsCancelButton = NO;
    [searchBar resignFirstResponder];
    
    [self.addFriendSearchView removeFromSuperview];
    [self showNavigationBar];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    
    DMLog(@"%@", searchText);
    NSArray *result = @[searchText];
    
    if ([searchText isEqualToString:@""]) {
        
        result = nil;
    }
    
    self.addFriendSearchView.results = result;
}

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];
    
    //关闭右滑返回
    _allowSideBack = NO;
    
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
}

- (void)viewDidDisappear:(BOOL)animated {

    [super viewDidDisappear:animated];
    
    //开启右滑返回
    _allowSideBack = YES;
    
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        
        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
}

//是否开启手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

    return _allowSideBack;
}

@end
